package com.tairanchina.md.meeting.resource;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.tairanchina.md.constants.CommonConstants;
import com.tairanchina.md.elife.rabbit.client.consumer.RabbitConsumerClient;
import com.tairanchina.md.elife.rabbit.client.producer.RabbitProducerClient;
import com.tairanchina.md.elife.rabbit.constants.ConnConstants;
import com.tairanchina.md.elife.rabbit.task.RabbitMQTask;
import com.tairanchina.md.meeting.constants.MeetingConstants;
import com.tairanchina.md.meeting.constants.MeetingProvider;
import com.tairanchina.md.meeting.dto.SunAwarderDto;
import com.tairanchina.md.meeting.dto.SunGoodDto;
import com.tairanchina.md.meeting.dto.SunStateDto;
import com.tairanchina.md.meeting.rabbit.SunStateTask;
import com.tairanchina.txRestFilter.engine.jersey.TxJerseyTools;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.annotation.Resource;
import javax.ws.rs.*;
import javax.ws.rs.core.Response;
import java.util.Date;

/**
 * 阳光普照模块
 */
@Path(MeetingConstants.PathRoute.SUN_ROOT)
public class SunResource
{
    private Logger logger = LoggerFactory.getLogger(SunResource.class);
    @GET
    public Response count()
    {
        /**
         * 类实现描述：TODO 签到人总数列表
         * hzyjie 16/12/24 11:56
         */
        Integer signNum = 0;
        try {
            signNum = MeetingProvider.userService.getSignNum();
        }catch (Exception e){
            e.printStackTrace();
            logger.error("查询当前签到人数失败,e:{}",e);
        }
        JSONObject json = new JSONObject();
        json.put("signNum",signNum);

        return TxJerseyTools.returnSuccess(json.toString());
    }

    /**
     * 返回当前服务器时间
     */
    @GET
    @Path("/time")
    public Response getTime(){
        Date date = new Date();
        JSONObject json = new JSONObject();
        json.put("time",date);
        return TxJerseyTools.returnSuccess(json.toString());
    }

    /**
     * 查询所有阳光普照奖品信息
     * @return
     */
    @GET
    @Path("/sunGoods")
    public Response getSunGoods(){
        SunGoodDto sunGoodDto = new SunGoodDto();
        try {

            sunGoodDto =  MeetingProvider.sunGoodsService.findAllSunGoods();
        }catch (Exception e){
             e.printStackTrace();
            logger.error("查询阳光奖品信息失败，e:{}",e);
            sunGoodDto.setSunGoodsNum(0);
        }
        return TxJerseyTools.returnSuccess(JSONObject.toJSONString(sunGoodDto));
    }

    /**
     * 查询所有的阳光普照中奖用户
     * @return
     */
    @GET
    @Path("/allAwarder")
    public Response getAllResponse(){
        SunAwarderDto sunAwarderDto = new SunAwarderDto();
        try {
            sunAwarderDto = MeetingProvider.sunAwarderService.findAllAwarder();
        }catch (Exception e){
            e.printStackTrace();
            logger.error("查询阳光奖品信息失败，e:{}",e);
            sunAwarderDto.setSunAwarderNum(0);
        }
        return TxJerseyTools.returnSuccess(JSONObject.toJSONString(sunAwarderDto));
    }

    @POST
    @Path("/sunState")
    public Response setSunState(@FormParam("sunState") String sunState){
        if(StringUtils.isBlank(sunState) || !check(sunState)){
            return TxJerseyTools.returnAbort(CommonConstants.ErrorCode.ERROR_ILLEGAL_PARAMTER);
        }
        SunStateDto sunStateDto = new SunStateDto();
        sunStateDto.setSunState(sunState);

        try {
            RabbitProducerClient producerClient = ConnConstants.getProducerClient();
            producerClient.sendMessage(MeetingConstants.EXCHANGE_NAME_SUN_STATE,MeetingConstants.QUEUE_KEY_SUN_STATE,sunState);

            RabbitConsumerClient consumerClient = ConnConstants.getConsumerClient();
            SunStateTask sunStateTask = new SunStateTask();
            consumerClient.receiveMessage(MeetingConstants.QUEUE_NAME_SUN_STATE_LOCAL,sunStateTask);
            sunStateDto.setCallBackState(sunStateTask.getState());

        }catch (Exception e){
            e.printStackTrace();
            logger.error("发送阳光普照抽奖状态失败,e:{}",e);
            return TxJerseyTools.returnServerError(CommonConstants.ErrorCode.ERROR_SERVICE_IN_REST);
        }
        return  TxJerseyTools.returnSuccess(JSON.toJSONString(sunStateDto));
    }

    private boolean check(String sunState) {
        if(MeetingConstants.SUN_START.equals(sunState) || MeetingConstants.SUN_RESET.equals(sunState)
                || MeetingConstants.SUN_STOP.equals(sunState)){
            return true;
        }
        return false;
    }


}
